<?php
global $Config;
require_once($Config->WebCMSRoot.'Base/Control.php');
require_once($Config->WebCMSRoot.'Models/Form.php');
require_once($Config->WebCMSRoot.'Models/MailForm.php');
require_once($Config->WebCMSRoot.'Models/Ticket.php');
require_once($Config->WebCMSRoot.'Views/MailForm.php');

Class MailForm extends BaseControl
{
	var $mailForm;
	var $view;
//	var $CustomCode;

	function MailForm($parameters, &$page)
	{
		$this->parameters = $parameters;

		if (isset($_POST['formID']) && isset($_SESSION[$_POST['formID']])
				|| isset($_POST['code'])
				|| ($this->ParameterExists('ticket') && $this->GetParameter('ticket') != ''))
		{
			$to = $this->ExtractFormParameter($this->GetParameter('to'));
			$from = $this->ExtractFormParameter($this->GetParameter('from'));
			$to = (strpos($to, ',') === FALSE) ? array(array($to,'')) : array(split(',', $to));
			$from = (strpos($from, ',') === FALSE) ? array($from,'') : split(',', $from);

			$this->mailForm = new MailFormModel($to, $from, $this->GetParameter('subject'), $this->GetParameter('template'));

			$this->view = new MailFormView();
//			$this->CustomCode = new FormCustomCode();
		}
		
		parent::BaseControl($parameters, &$page);
	}

	function Execute()
	{
//print('<pre>'.var_export($_POST,true).'</pre>');exit;
		if (isset($_POST['formID']) && isset($_SESSION[$_POST['formID']])
				|| isset($_POST['code'])
				|| ($this->ParameterExists('ticket') && $this->GetParameter('ticket') != ''))
		{
			$content = array();

			if (isset($_POST['formID']) && isset($_SESSION[$_POST['formID']]))
			{
				$form = $_SESSION[$_POST['formID']];
				
//print('<pre>'.var_export($this->parameters,true).'</pre>');//exit;
				if ($this->GetParameter('confirm'))
				{
				  $content['confirm_code'] = $_POST['formCode'];
				  $content['confirm_label'] = 'Ticket Code';
				}
			}
			else
			{
				if (isset($_POST['code']))
				{
					$ticketCode = $_POST['code'];
				}
				else
				{
					$ticketCode = $this->GetParameter('ticket');
				}
				
				$ticket = new TicketModel();
		        $ticketParameters = array('code' => $ticketCode);
	      		$ticket->Retrieve(&$ticketParameters);
//print('<pre>'.var_export($ticket,true).'</pre>');//exit;
				
				$form = new FormModel();
				$formParameters = array('id' => $ticket->Collection[0]->FormID);
				$form->Retrieve($formParameters);
				$form = $form->Collection[0];
//print('<pre>'.var_export($form,true).'</pre>');//exit;

				$formValues = new FormModel();
		        $formValueParameters = array('ID' => $ticket->Code);
	      		$formValues->RetrieveFormValues(&$formValueParameters);
//print('<pre>'.var_export($formValues->FormFields,true).'</pre>');//exit;

				$form->FormFields = $formValues->FormFields;

				$content['confirm_code'] = $ticketCode;
				$content['confirm_label'] = 'Ticket Code';
//print('<pre>'.var_export($form,true).'</pre>');//exit;
			}
			
			foreach($form->FormFields->Collection as $formField)
			{
				//print($formField->Label.': '.$formField->Value.'<br />');
				$content[$formField->Name.'_label'] = $formField->Label;
				$content[$formField->Name.'_value'] = $formField->Value;
				
			}
			$content['clientIP'] = $_SERVER['REMOTE_ADDR'];
			$content['timestamp'] = date('Y-m-d H:i:s');
			
			$this->mailForm->Content = $content;
			if (count($form->Errors) == 0)
			{
				if (strpos($_SERVER['SCRIPT_URI'], '/DEV') === false)
				{
//print('<pre>'.var_export($this->mailForm,true).'</pre>');exit;
					if ($this->mailForm->Send())
		  			{
						if ($this->GetParameter('feedback') == 'true')
						{
							$this->view->ShowFeedback('Uw formulier werd verstuurd.');
						}
		  			}
					else
					{
//print('An error occurred');exit;
						if ($this->GetParameter('feedback') == 'true')
						{
							$this->view->ShowFeedback('Er is een fout opgetreden bij het versturen van uw formulier, probeer a.u.b. opnieuw.');
						}
					}
				}
	  		}
//print('<pre>'.var_export($formField,true).'</pre>');exit;
		}
//print('<pre>'.var_export($_SESSION,true).'</pre>');//exit;

		parent::Execute();
	}

	function ValidateArguments()
	{
		$valid = (count($this->parameters) >= 1);
		$valid = $valid && ($this->ParameterExists('id') || $this->ParameterExists('ticket'));

		return $valid;
	}
	
	/* Extended Functions */
	function ExtractFormParameter($parameter)
	{
		$result = preg_match_all('|^FORM:(?P<field>\w*)|i', $parameter, $matches, PREG_SET_ORDER);
		if ($result >= 1)
		{
			$form = $_SESSION[$_POST['formID']];
//print('<pre>'.var_export($form,true).'</pre>');exit;
			$value = $form->FormFields->Collection[$matches[0]['field']]->Value;
		}
		else
		{
			$value = $parameter;
		}
		
		return $value;
	}
}
?>